<template>
  <div class="container">
    <!-- 左侧盒子 -->
    <div class="left">
      <!-- 树形图 -->
      <div class="tree">
        <el-tree
          ref="tree"
          :data="institutionList"
          :props="institutionProps"
          :expand-on-click-node="false"
          icon-class="el-icon-circle-plus"
          highlight-current
          node-key="id"
          :default-expanded-keys="defaultExpandedKeys"
          @node-click="nodeClick"
        />
      </div>
    </div>

    <!-- 右侧盒子 -->
    <div class="right">

      <el-card class="box-card">
        <div slot="header">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            style="height: 24px"
          >
            <span
              style="font-size: 16px; font-weight: 700; color: #2a2929"
            >机构范围</span>
            <el-button
              v-if="!isEdit && polygonPath.length !== 0"
              type="primary"
              @click="editMap"
            >编辑</el-button>
            <div v-if="isEdit">
              <el-button type="primary" @click="submitMapEdit">提交</el-button>
              <el-button @click="cancelMapEdit">取消</el-button>
            </div>
          </el-row>
        </div>
        <el-row>
          <span>地区关键词:</span>
          <el-input
            v-model="keyword"
            placeholder="请输入地区关键词"
            style="width: 200px; margin: 0 0 0 10px"
            @blur="search"
          />
          <div v-if="isEdit" class="edit">
            <el-button v-if="!mapEditing" type="primary" @click="startEditMap">
              开始绘制
            </el-button>
            <div v-if="mapEditing">
              <el-button
                type="primary"
                @click="mapEditing = false"
              >完成绘制</el-button>
              <el-button @click="delFencing">删除围栏</el-button>
            </div>
            <p>
              点击或拖动鼠标绘制作业范围，右击鼠标结束图形绘制，
              图形不可有交叉点。完成绘制后，点击提交按钮保存。
              每个机构仅可支持绘制一个作业范围
            </p>
          </div>
        </el-row>
        <!-- 百度地图 -->
        <baidu-map
          :scroll-wheel-zoom="true"
          :zoom="zoom"
          class="bm-view"
          ak="UsKDFdDAAtLy7r2oonffeSNcxq1mSvmV"
          :center="center"
          @ready="handler"
        >
          <bm-local-search
            :keyword="keyword"
            :auto-viewport="true"
            zoom="12.8"
            style="display: none"
          />
          <bm-polygon
            :path="polygonPath"
            stroke-color="#eec8cc"
            :stroke-opacity="0.6"
            :stroke-weight="1"
            stroke-style="dashed"
            :editing="mapEditing"
            fill-color="#eec8cc"
          /></baidu-map>
      </el-card>
    </div>
  </div>
</template>

<script>

import { getInstitutionList } from '@/api/institution'
import { getBusinessScopeApi } from '@/api/workingArea'
import BaiduMap from 'vue-baidu-map/components/map/Map.vue'
import BmPolygon from 'vue-baidu-map/components/overlays/Polygon'

export default {
  name: 'WorkingArea',
  components: {
    BaiduMap,
    BmPolygon
  },
  data() {
    return {
      // 树状机构数据
      institutionList: [],
      // 树状结构默认属性
      institutionProps: {
        label: 'name',
        children: 'children'
      },
      // 默认展开tree节点
      defaultExpandedKeys: [],
      // 搜索框
      serachIpt: '',
      // 百度地图坐标
      center: { lng: 0, lat: 0 },
      // 缩放
      zoom: 9,
      // 区域图
      polygonPath: [],
      // 折线图编辑
      mapEditing: false,
      // 是否编辑
      isEdit: false,
      // 搜索框
      keyword: ''
    }
  },
  async created() {
    // 获取树状机构信息
    await this.getInstitutionList()
    // 设置默认展开tree节点
    this.defaultExpandedKeys = [this.institutionList[0].children[0].id]
    // 设置默认选中
    const currentId = this.institutionList[0].children[0].children[0].id
    this.$refs.tree.setCurrentKey(currentId)
  },
  methods: {
    // 获取树状机构信息
    async getInstitutionList() {
      const result = await getInstitutionList()
      this.institutionList = JSON.parse(result.data.data)
    },
    // 获取业务范围
    async getBusinessScope(id, type) {
      // 发送请求
      const { data } = await getBusinessScopeApi(id, type)
      // 传递坐标
      this.$set(this.center, 'lng', data.longitude)
      this.$set(this.center, 'lat', data.latitude)
      // 传递绘画数组
      data.polygon.map((v) => {
        return this.polygonPath.push({
          lng: v.longitude,
          lat: v.latitude
        })
      })
    },

    // 节点被点击
    async chickNode(data) {
      // 清空原来的区域绘制数组,否则会追加push数据
      this.polygonPath = []
      // 获取业务范围
      await this.getBusinessScope(data.id, 1)
    },

    // 编辑状态
    editMap() {
      // 打开编辑状态
      this.isEdit = true
    },

    // 开始绘制地图
    startEditMap() {
      // 打开地图的编辑状态
      this.mapEditing = true
    },

    // 删除围栏
    delFencing() {
      // 提示用户
      this.$message.error('演示数据不允许修改')
      // 关闭地图的编辑状态
      this.mapEditing = false
    },

    // 提交地图编辑
    submitMapEdit() {
      // 提示用户
      this.$message.error('演示数据不允许编辑')
      // 关闭地图的编辑状态
      this.mapEditing = false
    },

    // 取消地图编辑
    cancelMapEdit() {
      // 关闭地图的编辑状态
      this.mapEditing = false
      // 关闭编辑状态
      this.isEdit = false
    },

    // 初始化地图
    handler(map) {
      this.map = map
    },

    search() {
      const localSearch = new this.map.BMap.LocalSearch(this.$refs.map, { input: this.serachIpt })
      localSearch.search()
    }
  }
}
</script>

<style lang="scss" scoped>
.container{
  padding-top: 20px;
  display: flex;
  justify-content: space-between;
  height: auto;
  background-color: #f3f4f7;
}
.left{
  margin-right: 20px;
  width: 220px;
  /* height: 100%; */
  background-color: #fff;
  border-bottom: 1px solid #ebeef5 ;
}
.right{
  flex: 1;
  /* display: flex; */
  /* justify-content: space-between; */
  /* flex-direction: column; */
  /* background-color: #8a8bf0; */
}
.workingAreaTitle{
  display: flex;
  justify-content: space-between;
  justify-content: space;
  padding-left: 10px;
  height: 50px;
  line-height: 50px;
  background-color: #f8faff;
  font-weight: 700;
  border-bottom: 1px solid #ebeef5 ;
}
.btn{
  padding-right: 10px;
}
.workMap{
  padding: 20px;
  background-color: #fff;
}
.keyword{
  padding-bottom: 10px;
}
.bm-view {
  width: 100%;
  height: 300px;
}
.wrapper {
  display: flex;
  .tree {
    flex: 3;
  }
  .row {
    flex: 21;
  }
}
.bm-view {
  margin-top: 20px;
  width: 100%;
  height: 668px;
}
.edit {
  background: rgb(255, 255, 255);
  width: 100%;
  position: absolute;
  top: 60px;
  z-index: 10;
  text-size-adjust: none;
  p {
    font-size: 12px;
    color: rgb(198, 126, 18);
    width: 100%;
    background: rgb(255, 245, 231);
    height: 30px;
    line-height: 30px;
    text-align: left;
    padding-left: 10px;
  }
}
</style>
